//The following example opens an output file for appending. Because no sharing mode is specified, the O_SHARE_RDWR sharing mode is used.

int fildes;
fildes = open("outfile",O_WRONLY | O_APPEND);

The following example creates a new file with read, write, and execute permissions for the user creating the file. If the file already exists, the open() fails. If the open() succeeds, the file is opened for sharing with readers only.

fildes = open("newfile",O_WRONLY|O_CREAT|O_EXCL|O_SHARE_RDONLY,S_IRWXU);

This example first creates an output file for with a specified CCSID. The file is then closed and opened again with data conversion. The open() function is called twice because no data conversion would have occurred when using the first open's descriptor on read or write operations, even if O_TEXTDATA had been specified on that open; however, the second open could be eliminated entirely by using O_TEXT_CREAT on the first open. This is demonstrated in the code example immediately following this example. In this example, EBCDIC data is written to the file and converted to ASCII.

#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
  int fd;
  int rc;
  char name[]="/test.dat";
  char data[]="abcdefghijk";
  int oflag1 = O_CREAT | O_RDWR | O_CCSID;
  int oflag2 = O_RDWR | O_TEXTDATA | O_CCSID;
  mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR;
  unsigned int file_ccsid = 819;
  unsigned int open_ccsid = 37;

  
  /*  First create the file with the CCSID 819.      */
 

  if ((fd=open(name,oflag1,mode,file_ccsid)) < 0)
  {
    perror("open() for create failed");
    return(0);
  }

  if (close(fd) < 0)
  {
    perror("close() failed.");
    return(0);  
  }

  
  /*  Now open the file so EBCDIC (CCSID 37) data    */
  /*  written will be converted to ASCII (CCSID 819).*/
  
  if ((fd=open(name,oflag2,mode,open_ccsid)) < 0)
  {
    perror("open() with translation failed");
    return(0);  
  }
  
  /* Write some EBCDIC data.                         */
  
  if (-1 == (rc=write(fd, data, strlen(data))))
  {
    perror("write failed");
    return(0);
  }

  if (0 != (rc=close(fd)))
  {
    perror("close failed");
    return(0);
  }
}
